var Project = {
    tableId: "projectTable"
};

function log(msg) {
    setTimeout(() => {
        throw new Error(msg);
    }, 0);
}

var numberPage = 1;

layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var func = layui.func;

    /* leez */
    var windowClientWidth = document.body.clientWidth
    var windowClientWidthTemp = document.body.clientWidth
    var tableIsReloading = false

    function _reloadTable() {
        if(tableIsReloading) return
        tableIsReloading = true
        windowClientWidth = document.body.clientWidth;
        if(Math.abs(windowClientWidth - windowClientWidthTemp) > 20) {
            windowClientWidthTemp = windowClientWidth
        }
        setTimeout(() => {
            tableIsReloading = false
        }, 1500)
    }

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", width: 50, minWidth: '40', hide: false, fixed: true},
            {field: 'name', sort: true, title: '名称', minWidth: '170', align: "left"},
            {field: 'yaoma', sort: true, title: '妖码', minWidth: '150', align: "left"},
            {field: 'yyjlname', sort: true, title: '营运经理', minWidth: '170', align: "left"},
            {field: 'ddname', sort: true, title: '督导', minWidth: '170', align: "left"},
            {field: 'openDate', sort: true, title: '开业时间', minWidth: '100', align: "left",templet:function(d){
                    return d.openDate.substring(0,10);
                }},
            {field: 'createUserName', sort: false, title: '开发人员', minWidth: '100',align: "left"},
            { title: '操作', minWidth: '130',align: "center",templet:function(d){
                    return '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>';
                }}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['provinceId'] = '216';//山西省
        queryData['cityId'] = $("#cityName").find("option:selected").val();
        queryData['name'] = $("#name").val();
        Project.tableCfg.where=queryData;
        table.render(Project.tableCfg);
    };

    /**
     * 弹出添加通知
     */
    Project.openAddDlg = function () {
        parent.layer.open({
            type: 2,
            title: '新增',
            area: ['90%', '650px'],
            content: Feng.ctxPath + '/project/shanxiAdd',
            end: function () {
                Project.search();
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Project.exportExcel = function () {
        var checkRows = table.checkStatus(Project.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/area/getAreaByPid");
    qyAjax.set("pid", 216);//山西省
    var qyResult = qyAjax.start();
    $("#cityName").html("");
    $("#cityName").append("<option value=''>全部</option>");
    $.each(qyResult, function (index, data) {
        $("#cityName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
    });
    form.render('select');


    Project.tableCfg={
        elem: '#' + Project.tableId,
        url:Feng.ctxPath + '/project/listFinish',
        // even: true,
        page:true,
        limit:20,
        height: "full-59",
        cellMinWidth: 0,
        cols: Project.initColumn(),
        where: {
            provinceId: '216'//山西省
        }
    };
    table.render(Project.tableCfg);

    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Project.search();
    });

    /**
     * 关店按钮点击事件
     */
    $('#btnEdit').click(function () {
        // $('#address').removeAttr('required');
        Project.editStatus();
    });

    /**
     * 添加按钮点击事件
     */
    $('#btnAdd').click(function () {
        Project.openAddDlg();
    });

    /**
     * 导出excel
     */
    $('#btnExp').click(function () {
        Project.exportExcel();
    });


    /**
     * 工具条点击事件
     */
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            parent.layer.open({
                type: 2,
                title: '修改',
                area: ['90%', '650px'],
                content: Feng.ctxPath + '/project/shanxiAdd?id='+data.id,
                end: function () {
                    Project.search();
                },
                cancel:function (index){
                    // Mdzx.search();
                }
            });
        }
    });



});
